/* 2014/7/28 */
package cosmos.media;

import java.util.Arrays;

/**
 * Represents ID3V1 tag.
 * @author Leonardo
 *
 */
public class ID3v1 {

	public static final int TOTAL_Length = 128;
    public static final int HeaderLength = 3;
    public static final int TitleLength = 30;
    public static final int ArtistLength = 30;
    public static final int AlbumLength = 30;
    public static final int YearLength = 4;
    public static final int CommentLength = 28;
    
    private static final byte[] AVAILABLE_HEADER = 
            new byte[] { (byte)'T', (byte)'A', (byte)'G' };
    
    private byte[] header;
    
    private byte[] title;
    
    private byte[] artist;
    
    private byte[] album;
    
    private byte[] year;
    
    private byte[] comment;
    
    private byte[] reserve;
    
    private byte[] track;
    
    private byte[] genre;
    
    public ID3v1() {
    	header = new byte[HeaderLength];
        title = new byte[TitleLength];
        artist = new byte[ArtistLength];
        album = new byte[AlbumLength];
        year = new byte[YearLength];
        comment = new byte[CommentLength];
    }
    
    public void load(byte[] bytes) {
    	int i;
        int j;
        int position = 0;
        int currentIndex = 0;

        // Extracts header.
        for (i = currentIndex, j = 0; i < currentIndex + ID3V1.HeaderLength;
            i++, j++)
        {
            Header[j] = bytes[i];
            position++;
        }
        currentIndex = position;

        // Extracts title.
        for (i = currentIndex, j = 0; i < currentIndex + ID3V1.TitleLength;
            i++, j++)
        {
            Title[j] = bytes[i];
            position++;
        }
        currentIndex = position;

        // Extracts artist.
        for (i = currentIndex, j = 0; i < currentIndex + ID3V1.ArtistLength;
            i++, j++)
        {
            Artist[j] = bytes[i];
            position++;
        }
        currentIndex = position;

        // Extracts album.
        for (i = currentIndex, j = 0; i < currentIndex + ID3V1.AlbumLength;
            i++, j++)
        {
            Album[j] = bytes[i];
            position++;
        }
        currentIndex = position;

        // Extracts year.
        for (i = currentIndex, j = 0; i < currentIndex + ID3V1.YearLength;
            i++, j++)
        {
            Year[j] = bytes[i];
            position++;
        }
        currentIndex = position;

        // Extracts comments.
        for (i = currentIndex, j = 0; i < currentIndex + ID3V1.CommentLength;
            i++, j++)
        {
            Comment[j] = bytes[i];
            position++;
        }
        currentIndex = position;

        // Extracts reserved bits. 
        Reserve = bytes[++position];
        Track = bytes[++position];
        Genre = bytes[++position];
    }
    
    public boolean isAvailable() {
		return Arrays.equals(header, AVAILABLE_HEADER);
	}
}
